﻿using System.Threading.Tasks;

using AutoMapper;

using KSOAdmin.IServices.System;
using KSOAdmin.Models.Core;
using KSOAdmin.Models.DomainModels.System;
using KSOAdmin.Models.ViewModel;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace KSOAdmin.WebApi.Controllers
{
    /// <summary>
    /// 表格
    /// </summary>
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "表格")]
    public class TableController : BaseApiControllers
    {
        private readonly ISys_LogServices sys_Log;
        private readonly IMapper _mapper;

        public TableController(ISys_LogServices _sys_Log, IMapper mapper)
        {
            this.sys_Log = _sys_Log;
            this._mapper=mapper;
        }

        /// <summary>
        /// 表格数据样式获取
        /// </summary>
        /// <param name="viewsys_Log"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PageList/{pageIndex}/{pageSize}")]
        public async Task<ResponseModel> PageListAsync([FromRoute] int pageIndex, [FromRoute] int pageSize, [FromBody] View_Sys_Log viewsys_Log)
        {
            return ResponseModel.Success("成功", await sys_Log.SysLogPageListAsync(viewsys_Log, pageIndex, pageSize));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Delete")]
        public async Task<ResponseModel> DeleteAsync([FromBody] string[] keys)
        { 
            return ResponseModel.Success("成功",await sys_Log.Del(keys,true));
        }

        /// <summary>
        /// 修改或者增加
        /// </summary>
        /// <param name="viewsys_Log"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateOrAdd")]
        public async Task<ResponseModel> UpdateOrAddAsync([FromBody] View_Sys_Log viewsys_Log)
        {
            Sys_Log sys_log=_mapper.Map<Sys_Log>(viewsys_Log);
            if ( viewsys_Log?.ID==0)
            {
                return ResponseModel.Success("成功", await sys_Log.AddEntity(sys_log, true));
            }
            else
            {
                return ResponseModel.Success("成功", await sys_Log.Upload(sys_log));
            }
        }

        /// <summary>
        /// 获取某一条的详情
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Details/{ID}")]
        public async Task<ResponseModel> DetailsAsync([FromRoute]int ID)
        {
            return ResponseModel.Success("成功" ,await sys_Log.FindFirstAsync(w => w.ID == ID));
        }

        [HttpPost]
        [Route("ImportFile")]
        public ResponseModel ImportFile()
        {
            return null;
        }


    }
}
